package models

import (
	"chinasoccer/pkg/utils"
	"fmt"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// 批量录入商品数据，手动输入商品编号，商品名称，商品单价，供应商编号，商品简介，备注
type Good struct {
	gorm.Model
	GoodsID         string `json:"goodsid" form:"goodsid" gorm:"type:varchar(20);not null;unique;" binding:"required"`
	GoodsName       string `json:"goodsname" form:"goodsname" gorm:"type:varchar(20);not null; unique" binding:"required"`
	GoodsPrice      string `json:"goodsprice" form:"goodsprice" gorm:"type:varchar(20);not null;" binding:"required"`
	GoodsSupplierID string `json:"goodssupplierid" form:"goodssupplierid" gorm:"type:varchar(20);not null;" binding:"required"`
	GoodsDocuments  string `json:"goodsdocuments" form:"goodsdocuments" gorm:"type:varchar(20);not null;" binding:"required"`
	GoodsOthers     string `json:"goodsothers" form:"goodsothers" gorm:"type: varchar(20);not null;" binding:"required"`
	IsDel           bool   `json:"isdel" form:"isdel" gorm:"type:boolean default false;"`
}

// goodsid goodsname goodsprice goodssupplierid goodsdocuments goodsothers
func getJson2(c *gin.Context) Good {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)
	goodsID := json["goodsid"]
	goodsName := json["goodsname"]
	goodsPrice := json["goodsprice"]
	goodsSupplierID := json["goodssupplierid"]
	goodsDocuments := json["goodsdocuments"]
	goodsOthers := json["goodsothers"]
	goods := Good{
		GoodsID:         goodsID,
		GoodsName:       goodsName,
		GoodsPrice:      goodsPrice,
		GoodsSupplierID: goodsSupplierID,
		GoodsDocuments:  goodsDocuments,
		GoodsOthers:     goodsOthers,
	}
	return goods
}

// 获取所有的 Goods
func GetAllGoods(goodsParam map[string]interface{}) ([]Good, int64, error) {
	var goodsdata []Good
	pagenum := goodsParam["pagenum"].(int)
	pagesize := goodsParam["pagesize"].(int)
	query := goodsParam["query"].(string)
	if query == "" {
		var total int64
		err := db.Table("goods").Where("is_del = false").Count(&total).Error
		if err != nil {
			return goodsdata, 0, err
		}
		err = db.Table("goods").Where("is_del = false").Offset((pagenum - 1) * pagesize).Limit(pagesize).Find(&goodsdata).Error
		fmt.Println("total = ", total)
		return goodsdata, total, err
	}
	goodsName := query

	var goods Good
	err := db.Table("goods").Where("goods_id = ?", goodsName).First(&goods).Error
	goodsdata = append(goodsdata, goods)
	if err != nil {
		return goodsdata, 0, err
	}

	return goodsdata, 1, err
}

// 增加商品的信息
func AddGoods(c *gin.Context) (Good, int64, error) {
	goods := getJson2(c)
	var total int64
	db.Table("goods").Where("goods_id = ?", goods.GoodsID).Count(&total)
	if total != 0 {
		return Good{}, total, nil
	}
	err := db.Table("goods").Create(&goods).Error
	return goods, total, err
}

// 修改员工信息
func UpdateGoods(c *gin.Context) (Good, error) {
	goods := getJson2(c)
	err := db.Table("goods").Where("goods_id = ?", goods.GoodsID).Updates(&goods).Error
	return goods, err
}

// 删除员工
func DelGoods(c *gin.Context) error {
	id := c.Params.ByName("id")
	err := db.Table("goods").Where("goods_id = ?", id).Update("is_del", true).Error
	if err != nil {
		return err
	}
	err = db.Table("goods").Where("goods_id = ?", id).Update("goods_id", utils.RandomString(19)).Error
	return err
}
